//
//  LXStatusToolBarView.m
//  微了个小博
//
//  Created by imera on 15/10/10.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXStatusToolBarView.h"
#import "LXStatus.h"

/**分割线宽度*/
#define LXStatusDividerWidth 2

@interface LXStatusToolBarView()
@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) NSMutableArray *dividerArr;
/** 微博的转发数 */
@property (nonatomic, weak) UIButton *repostBtn;
/** 微博的评论数 */
@property (nonatomic, weak) UIButton *commentsBtn;
/** 微博的表态数(被赞数)*/
@property (nonatomic, weak) UIButton *attitudesBtn;
@end

@implementation LXStatusToolBarView

- (NSMutableArray *)btnArr
{
    if(_btnArr == nil){
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}

- (NSMutableArray *)dividerArr
{
    if(_dividerArr == nil){
        _dividerArr = [NSMutableArray array];
    }
    return _dividerArr;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        
        self.userInteractionEnabled = YES;
        self.image =[UIImage resizeWithName:@"timeline_card_bottom_background"];
        self.highlightedImage = [UIImage resizeWithName:@"timeline_card_bottom_background_highlighted"];

        //1.初始化工具条按钮
        self.repostBtn = [self setupBtnWithTitle:@"转发" image:@"timeline_icon_retweet" bgImage:@"timeline_card_leftbottom_highlighted"];
        self.commentsBtn = [self setupBtnWithTitle:@"评论" image:@"timeline_icon_comment" bgImage:@"timeline_card_middlebottom_highlighted"];
        self.attitudesBtn = [self setupBtnWithTitle:@"赞" image:@"timeline_icon_unlike" bgImage:@"timeline_card_rightbottom_highlighted"];

        //2.初始化分割线
        [self setupDivider];
        [self setupDivider];
    }
    return self;
}

/**
 *  初始化工具条按钮
 *
 *  @param title   按钮标题
 *  @param image   按钮图片
 *  @param bgImage 背景图片
 *
 *  @return 按钮
 */
- (UIButton *)setupBtnWithTitle:(NSString *)title image:(NSString *)image bgImage:(NSString *)bgImage
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageWithName:image] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage resizeWithName:bgImage] forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.adjustsImageWhenHighlighted = NO;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    
    [self addSubview:btn];
    [self.btnArr addObject:btn];
    return btn;
}

- (void)setupDivider
{
    UIImageView *divider = [[UIImageView alloc]init];
    divider.image = [UIImage imageWithName:@"timeline_card_bottom_line"];
    [self addSubview:divider];
    [self.dividerArr addObject:divider];
}

- (void)layoutSubviews
{
    //1.初始化按钮Frame
    CGFloat num = self.btnArr.count;
    CGFloat btnW = (self.frame.size.width - LXStatusDividerWidth * 2) / num;
    CGFloat btnH = self.frame.size.height;
    CGFloat btnY = 0;

    for(int i=0;i<num;i++){
        UIButton *btn = self.btnArr[i];
        CGFloat btnX = i * (btnW + LXStatusDividerWidth);
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
    }
    
    //2.初始化分割线Frame
    CGFloat dividerW = LXStatusDividerWidth;
    CGFloat dividerH = self.frame.size.height;
    CGFloat dividerY = 0;

    for(int i=0;i<self.dividerArr.count;i++){
        UIImageView *divider = self.dividerArr[i];
        CGFloat dividerX = i * (dividerW + btnW) + btnW;
        divider.frame = CGRectMake(dividerX, dividerY, dividerW, dividerH);
    }
    [super layoutSubviews];
    
}

- (void)setStatus:(LXStatus *)status
{
    _status = status;

    [self setupBtn:self.repostBtn origianlTitle:@"转发" count:status.reposts_count];
    [self setupBtn:self.commentsBtn origianlTitle:@"评论" count:status.comments_count];
    [self setupBtn:self.attitudesBtn origianlTitle:@"赞" count:status.attitudes_count];
}

- (void)setupBtn:(UIButton *)btn origianlTitle:(NSString *)originalTitle count:(int)count
{
    if(count){
        [btn setTitle:[NSString stringWithFormat:@"%d",count] forState:UIControlStateNormal];
    }else{
        [btn setTitle:originalTitle forState:UIControlStateNormal];
    }
}
@end
